/*
 * PayModel.java
 * business
 *
 * Created by ChenTao on 2017/2/28.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Order;
import com.yidiandao.business.data.request.QueryPayInfoRequest;
import com.yidiandao.business.data.response.QueryOrderResultResponse;
import com.yidiandao.business.data.response.QueryPayInfoResponse;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.Functional;
import com.yidiandao.business.ui.widget.listview.displaylistitem.OrderInformationBankInfoListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.OrderInformationResultInfoListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem
        .OrderInformationResultStatisticsListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 支付Model
 */
public class PayModel implements IPayModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<DisplayListItem> bankTransferListItems = new ArrayList<>();

    @Override
    public void loadPayInfo(
            String orderPayId,
            String orderId,
            String tag,
            final OnAccessNetListener<QueryPayInfoResponse> listener) {

        QueryPayInfoRequest request = new QueryPayInfoRequest();
        request.setUrl(Urls.GET_PAY_INFO);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setOrderPayId(orderPayId);
        request.setOrderId(orderId);

        mNetBiz.sendGetRequest(request, QueryPayInfoResponse.class,
                new NetListener<QueryPayInfoResponse>() {
                    @Override
                    public void onSuccess(QueryPayInfoResponse response) {
                        if (listener != null) {
                            listener.onSuccess(response, true);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void loadBankTransferOrderInfo(
            String orderId, String tag, final OnAccessNetListener<List<DisplayListItem>> listener) {

        QueryPayInfoRequest request = new QueryPayInfoRequest();
        request.setUrl(Urls.GET_PAY_INFO);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setOrderId(orderId);

        mNetBiz.sendGetRequest(request, QueryOrderResultResponse.class,
                new NetListener<QueryOrderResultResponse>() {
                    @Override
                    public void onSuccess(QueryOrderResultResponse response) {

                        bankTransferListItems.clear();

                        QueryOrderResultResponse.Data data = response.getData();

                        // 银行转账的订单，显示银行转账信息
                        final List<QueryOrderResultResponse.Data.PayDesc> payDescList = data.getPayDesc();
                        if (payDescList != null && payDescList.size() != 0) {
                            // 最后一个：注意事项 向左对齐
                            payDescList.get(payDescList.size() - 1).setAlignLeft(true);
                            List<DisplayListItem> bankListItems = Functional.serverToClient(payDescList, new Functional.Processor<QueryOrderResultResponse.Data.PayDesc, DisplayListItem>() {
                                @Override
                                public DisplayListItem processToClient(QueryOrderResultResponse.Data.PayDesc payDesc) {
                                    return new OrderInformationBankInfoListItem(payDesc);
                                }
                            });
                            bankTransferListItems.addAll(bankListItems);
                        }

                        List<Order> orderList = data.getOrderList();

                        // 订单信息
                        List<DisplayListItem> orderInfoListItems = Functional.serverToClient(orderList, new Functional.Processor<Order, DisplayListItem>() {
                            @Override
                            public DisplayListItem processToClient(Order orderBean) {
                                return new OrderInformationResultInfoListItem(orderBean);
                            }
                        });
                        // 添加所有订单信息
                        bankTransferListItems.addAll(orderInfoListItems);

                        // 订单统计信息 - 付款金额/方式  收货人/地址
                        bankTransferListItems.add(new OrderInformationResultStatisticsListItem(data));

                        //确认付款button是否可用
                        int payButton = data.getPayButton();
                        boolean enable = payButton == 1;

                        if (listener != null) {
                            listener.onSuccess(bankTransferListItems, enable);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void confirmPay(String orderId, String tag, final OnConfirmPayListener listener) {
        QueryPayInfoRequest request = new QueryPayInfoRequest();
        request.setUrl(Urls.FINISH_BANK_PAY);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setOrderId(orderId);
        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }
}
